#include <iostream>
using namespace std;
class Country
{
    public:
        Country();
        void buy(int pMoney);
        void sell(int pMoney);
        void setId(int id);
        void show();
        int id;
        int money;
};
Country::Country()
{
    money = 0;
}
void Country::buy(int pMoney)
{
     money = money - pMoney;
}

void Country::sell(int pMoney)
{
     money = money + pMoney;
}
void Country::show()
{
    cout<<id<<" "<<money<<endl;
}
void Country::setId(int pid)
{
    id = pid;
}
int main()
{
   int N;
   cin >> N;
   Country * all = new Country[N+1];

   for(int i = 1; i<=N; i++)
   {
       all[i].setId(i);
       int num;
       cin>>num;
       for(int j = 0; j<num; j++)
       {
           int cid,money;
           cin>>cid>>money;
           all[i].sell(money);
           all[cid].buy(money);
       }
   }
   bool bIfSwap = false;
   while(true)
   {
       bIfSwap = false;
       for(int i = 1; i<=N-1; i++)
       {
           if(all[i].money< all[i+1].money)
           {
               bIfSwap = true;
               Country temp = all[i];
               all[i] = all[i+1];
               all[i+1]= temp;
           }
       }
       if(bIfSwap == false)
           break;
   }

   for(int i = 1; i<=N; i++)
         all[i].show();

   delete[] all;

}